package net.tv90.bfmccore.listener;

import net.tv90.bfmccore.util.Cooldown;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import static net.tv90.bfmccore.helper.FactionHelper.getTeamer;
import static net.tv90.bfmccore.helper.MCHHelper.*;

public class CooldownListener implements Listener {

    @EventHandler
    public void playerDeath(PlayerDeathEvent e) {
        Player p = e.getEntity();
        String vehicleFaction = getFactionVehicle(p);
        //如果玩家不是步兵身份
        if(!vehicleFaction.endsWith("INF")){
            //玩家自身的冷却
            Cooldown cd = new Cooldown(vehicleFaction, getVehicleCooldownTime(vehicleFaction), p);
            //冷却开始计时
            cd.startForSinglePlayer();
            //检测到玩家死亡后，设定为步兵身份
            setINF(p);
            for (Player teamer : getTeamer(p)) {
                teamer.sendMessage("§a[队伍]队友" + p.getName() + "放弃了" + getVehicle_Lang_2(vehicleFaction));
            }
        }
    }

    @EventHandler
    public void playerQuit(PlayerQuitEvent e) {
        Player p = e.getPlayer();
        String vehicleFaction = getFactionVehicle(p);
        if(!vehicleFaction.endsWith("INF")){
            Cooldown cd = new Cooldown(vehicleFaction, getVehicleCooldownTime(vehicleFaction), p);
            cd.startForSinglePlayer();
            setINF(p);
            for (Player teamer : getTeamer(p)) {
                teamer.sendMessage("§a[队伍]队友" + p.getName() + "放弃了" + getVehicle_Lang_2(vehicleFaction));
            }
        }
    }
}